package com.rrjjg.app.core.interceptor;

import com.rrjjg.app.core.sign.JwtHelper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.val;
import org.springframework.web.servlet.HandlerInterceptor;

import static com.rrjjg.app.core.Constants.TOKEN;

/**
 * ----------------------------------------------------------------------------- <br>
 * 功能 :  <br>
 * ----------------------------------------------------------------------------- <br>
 * 作者 : Vayne <br>
 * 时间 : 2023/4/12 <br>
 */
public class SignInterceptor implements HandlerInterceptor {

    private JwtHelper jwtHelper;

    public SignInterceptor(JwtHelper jwtHelper) {
        this.jwtHelper = jwtHelper;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(TOKEN);
        val o = jwtHelper.doCheck(token);
        jwtHelper.saveOnce(o);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        jwtHelper.clearOnce(null);
    }


    public String [] getExclude() {
        return jwtHelper.exclude();
    }
}
